# ruby_fetion/protocol.rb
# -*- encoding: utf-8 -*-
# License: GPL v2
# Author:  jhsea3do@gmail.com
# Version: 0.1

class FetionProtocol

  CRLF       = "\r\n"
  SIPC_VER   = "SIP-C/2.0"
  CLIENT_VER = '3.2.0540'

  def self.RESP_2XX
    { 200 => "#{SIPC_VER} 200 OK",
      280 => "#{SIPC_VER} 280 Send SMS OK" }
  end

  def self.RESP_4XX
    { 401 => "#{SIPC_VER} 401 Unauthoried" }
  end

  def self.GET_SYSTEM_CONFIG
  %{<config>
    <user mobile-no="%s" />
    <client type="PC" version="#{CLIENT_VER}" platform="W5.1" />
    <servers version="0" />
    <service-no version="0" />
    <parameters version="0" />
    <hints version="0" />
    <http-applications version="0" />
    <client-config version="0" /></config>} 
  end

  # LOGIN
  def self.LOGIN
  %{<args>
    <device type="PC" version="0" client-version="#{CLIENT_VER}" />
    <caps value="simple-im;im-session;temp-group;personal-group" />
    <events value="contact;permission;system-message;personal-group" />
    <user-info attributes="all" />
    <presence><basic value="%s" desc="" /></presence></args>}
  end
  def self.LOGIN_CMDS
    cmd0 = %{R %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s R#{CRLF}L: %s#{CRLF}#{CRLF}}
    cmd1 = %{R %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s R#{CRLF}A: Digest response="%s",cnonce="%s"#{CRLF}L: %s#{CRLF}#{CRLF}}
    [cmd0, cmd1]
  end
  def self.LOGIN_SUCC
    self.RESP_2XX[200]
  end

  # LOGOUT
  def self.LOGOUT; end
  def self.LOGOUT_CMDS
    cmd0 = %{R %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s R#{CRLF}X: 0#{CRLF}#{CRLF}}
    [cmd0]
  end
  def self.LOGOUT_SUCC
    self.RESP_2XX[200]
  end

  # SEND_SMS
  def self.SEND_SMS; end
  def self.SEND_SMS_CMDS
    cmd0 = %{M %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s M#{CRLF}T: %s#{CRLF}N: SendSMS#{CRLF}L: %s#{CRLF}#{CRLF}}
    cmd1 = %{M %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s M#{CRLF}T: %s#{CRLF}N: SendCatSMS#{CRLF}L: %s#{CRLF}#{CRLF}}
    [cmd0, cmd1]
  end
  def self.SEND_SMS_SUCC
    self.RESP_2XX[280]
  end

  # SEND_MSG
  def self.SEND_MSG; end
  def self.SEND_MSG_CMDS; end
  def self.SEND_MSG_SUCC; end

  # GET_CONTACT_LIST
  def self.GET_CONTACT_LIST
  %{<args>
    <contacts>
      <buddy-lists /><buddies attributes="all" />
      <mobile-buddies attributes="all" /><chat-friends />
      <blacklist /><allow-list />
    </contacts></args>}
  end
  def self.GET_CONTACT_LIST_CMDS
    cmd0 = %{S %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s S#{CRLF}N: GetContactList#{CRLF}L: %s#{CRLF}#{CRLF}}
    [cmd0]
  end

  # GET_OFFLINE_MESSAGES
  def self.GET_OFFLINE_MESSAGES; end
  def self.GET_OFFLINE_MESSAGES_CMDS
    cmd0 = %{S %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s S#{CRLF}N: GetOfflineMessages#{CRLF}#{CRLF}}
    [cmd0]
  end

  # KEEP_ALIVE
  def self.KEEP_ALIVE; end
  def self.KEEP_ALIVE_CMDS
    cmd0 = %{R %s #{SIPC_VER}#{CRLF}F: %s#{CRLF}I: %s#{CRLF}Q: %s R#{CRLF}#{CRLF}}
    [cmd0]
  end

  # REG

  # INFO
  
  # RECV
  #
=begin

>>>
I 669635763 SIP-C/2.0
F: sip:980736469@fetion.com.cn;p=2264
I: -1
K: text/plain
K: text/html-fragment
K: multiparty
K: nudge
Q: 2 I
L: 21

s=session
m=message 
>>>


>>>
SIP-C/2.0 200 OK
I: 50
Q: 48 R
X: 600

M 669635763 SIP-C/2.0
F: sip:980736469@fetion.com.cn;p=2264
I: -3
Q: 4 M
C: text/plain
K: SaveHistory
L: 6
XI: b086fb71-cb42-4f5d-add8-85031049ffde
D: Sat, 20 Mar 2010 14:09:45 GMT

你好

SIP-C/2.0 200 OK
I: 51
Q: 49 R
X: 600


>>>



=end
end

